﻿
$(function () {
    //初始化列表信息
    init();
});

//初始化列表信息
function init(queryData) {
    $('#list').datagrid({
        title: '备忘录',
        idField: 'MemoryID',
        fitColumns: 'true',
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: 'Memory/GetList',
        singleSelect: true,
        columns: [[
			//{ field: 'ck', checkbox: true },
			{ field: 'MemoryID', title: 'MemoryID', hidden: true },
			{ field: 'Content', title: '内容', width: 500 },
            //{
            //    field: 'RemindType', title: '提醒类型', width: 180,
            //    formatter: function (value, rec) {
            //        return rec.RemindDTO["RemindType"];
            //    }
            //},
            {
                field: 'RemindMethod', title: '提醒方式', width: 180,
                formatter: function (value, rec) {
                    return rec.RemindDTO["RemindMethod"];
                }
            },
            {
                field: 'Detail', title: '提醒时间', width: 320,
                formatter: function (value, rec) {
                    return rec.RemindDTO["Detail"];
                }
            },
            //{
            //    field: 'RemindTime', title: '提醒时间', width: 180,
            //    formatter: function (value, rec) {
            //        return formatDate(rec.RemindDTO["RemindTime"], "yyyy-MM-dd HH:mm");
            //    }
            //},
			{ field: 'CreateByName', title: '创建人', width: 100 },
			{
			    field: 'CreateTime', title: '创建时间', width: 100,
			    formatter: function (value, row, index) {
			        return formatDate(value, "yyyy-MM-dd");
			    }
			},
			//{ field: 'UpdateBy', title: '修改人', width: 180},
			//{ field: 'UpdateTime', title: '修改时间', width: 180},
        ]],
        //onSelect: function (rowIndex, rowData) {
        //    $("#selectedID").val($("#selectedID").val() + "," + rowData["MemoryID"]);
        //    alert($("#selectedID").val());
        //},
        //onUnselect: function (rowIndex, rowData) {
        //    var arr = $("#selectedID").val().split(",");
        //    arr.splice($.inArray(rowData["MemoryID"], arr), 1);
        //    $("#selectedID").val(arr);
        //    alert($("#selectedID").val());
        //}
    });
}

//弹出增加窗口
function add() {
    //获取新MemoryID
    $.getJSON("Memory/GetNewGuid?date=" + new Date().getTime(), function (data) {
        $("#MemoryID").val(data);
        $("#frmRemind").attr("src", "Remind/Index?objectID=" + $("#MemoryID").val() + "&RemindUserID=" + $("#RemindUserID").val());
    });
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    var editor = CKEDITOR.instances.editor;
    editor.setData('');
    url = '/Memory/Create';

}

//弹出修改窗口
function edit() {
    var row = $('#list').datagrid('getSelected');
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        $('#fm').form('load', row);
        var editor = CKEDITOR.instances.editor;
        editor.setData(row["Content"]);
        url = '/Memory/Edit';
        $("#frmRemind").attr("src", "Remind/Index?objectID=" + row["MemoryID"] + "&RemindUserID=" + $("#RemindUserID").val());//把修改的MemoryID传到提醒
    }
}

//保存数据
function save() {
    var editor = CKEDITOR.instances.editor;
    var text4 = editor.getData();
    $('#Content').val(text4);
    if ($('#Content').val() == "") {
        alert("请输入内容！");
        return;
    }
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            //keditor.sync();// 编辑器值回写，这里的keditor 要和 div的  data-options 里建立的一样；
            return $("#fm").form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                if (url.indexOf("Create") >= 0)
                    $(window.frames["frmRemind"].document).find("#create").click();//调用提醒功能保存提醒模块
                else (url.indexOf("Edit") >= 0)
                $(window.frames["frmRemind"].document).find("#edit").click();//调用提醒功能保存提醒模块
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/Memory/Delete', { id: rows.MemoryID },
                    function (result) {
                        if (result == 'OK') {
                            //$(window.frames["frmRemind"].document).find("#del").click();//调用提醒功能保存提醒模块
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}